<template>
  <div>
    <Form :label-width="80">
      <Card :bordered="false" :dis-hover="true">
        <Row>
          <Col span="14" offset="5">
            <FormItem label="标题：">
              <Input v-model="formItem.title" placeholder="请输入内容..."></Input>
            </FormItem>
          </Col>
        </Row>
        <Row>
          <Col span="14" offset="5">
            <FormItem label="内容：">
              <Input v-model="formItem.content" type="textarea" :autosize="{minRows: 28,maxRows: 40}" placeholder="请输入内容..."></Input>
            </FormItem>
          </Col>
        </Row>
      </Card>
      <Card>
        <Row>
          <Col span="14" offset="5">
            <p align="right">
              <Button type="info" @click="save">保存</Button>
            </p>
          </Col>
        </Row>
      </Card>
    </Form>
  </div>
</template>
<script>
    export default {
        components: {
        },
        data () {
            return {
                baseURL: '/user/announce',
                formItem: {
                    id: '',
                    title: '',
                    content: ''
                }
            }
        },
        methods: {
            search () {
                let url = this.baseURL + '/find'
                this.$ajax.get(url).then(r => {
                    let {code, data, msg} = r.data
                    if (code === 0) {
                        this.formItem.id = data.id
                        this.formItem.title = data.title
                        this.formItem.content = data.content
                    } else {
                        this.$bus.alert('error', msg)
                    }
                })
            },
            save () {
                let url = this.baseURL + '/saveOrUpdate'
                this.$ajax.post(url, this.formItem).then(r => {
                    let {code, msg} = r.data
                    if (code === 0) {
                        this.$bus.alert('success', '保存成功')
                        this.search()
                    } else {
                        this.$bus.alert('error', msg)
                    }
                })
            },
            init () {
                this.search()
            }
        },
        mounted () {
            this.init()
        },
        watch: {
            '$route' (to) {
                if (to.path === '/announcement') {
                    this.search()
                }
            }
        }
    }
</script>